home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / HyperCard CTB Toolkit 1.0b2 / Source Code / CTBNewTerminal.p < prev    next >
Encoding:
Text File  |  1995-02-07  |  3.8 KB  |  130 lines  |  [TEXT/MPS ]

  1. (*
  2.     CTBNewTerminal [tool] -- Make a new terminal tool. If one is open, dispose of it. The tool
  3.         parameter is the initial tool name to use.
  4.  
  5.     To compile and link this file using Macintosh Programmer's Workshop,
  6.  
  7.         pascal -w CTBNewTerminal.p
  8.         link -m ENTRYPOINT -o HyperCommands -rt XCMD=2759 -sn Main=CTBNewTerminal ∂
  9.             CTBNewTerminal.p.o "{MPW}"Libraries:interface.o "{MPW}"Libraries:Libraries:HyperXLib.o
  10.  
  11.     © Copyright 1990 by Apple Computer, Inc.
  12.  
  13.     Initial coding 2/90 by Harry R. Chesley.
  14. *)
  15.  
  16. {$R-}
  17.  
  18. {$S CTBNewTerminal }     { Segment name must be the same as the command name. }
  19.  
  20. unit DummyUnit;
  21.  
  22. interface
  23.  
  24. uses MemTypes, QuickDraw, Windows, OSIntf, ToolIntf, CTBUtils, FTIntf, CMIntf, TMIntf, CRMIntf,
  25.             HyperXCmd;
  26.  
  27. procedure EntryPoint(paramPtr: XCmdPtr);
  28.     
  29. implementation
  30.  
  31. procedure CTBNewTerminal(paramPtr: XCmdPtr); forward;
  32.  
  33. procedure EntryPoint(paramPtr: XCmdPtr);
  34.  
  35.     begin
  36.         CTBNewTerminal(paramPtr);
  37.     end;
  38.  
  39. procedure CTBNewTerminal(paramPtr: XCmdPtr);
  40.  
  41.     {$I CTBUtil.inc}
  42.  
  43.     var i, j: integer;
  44.         r: Rect;
  45.         toolName: Str255;
  46.         procID: integer;
  47.         tHand: TermHandle;
  48.         gWind: WindowPtr;
  49.         h: Handle;
  50.  
  51.     procedure Fail(errMsg: Str255); { set theResult and quit }
  52.         begin
  53.             paramPtr^.returnValue := PasToZero(paramPtr,errMsg);
  54.             exit(CTBNewTerminal);
  55.         end;
  56.  
  57.     begin
  58.         { Check for a valid parameter count. }
  59.         if paramPtr^.paramCount > 1 then Fail('Invalid parameter count');
  60.  
  61.         { Ensure we're loaded and ready. }
  62.         CTBReady;
  63.  
  64.         { If there's already a terminal open, close it. }
  65.         if Globals^^.termHand <> nil then
  66.             begin
  67.                 { Dispose of the terminal emulator. }
  68.                 gWind := Globals^^.termHand^^.owner;
  69.                 h := Handle(TMGetUserData(Globals^^.termHand));
  70.                 TMDispose(Globals^^.termHand);
  71.                 { Get rid of our invisible window. }
  72.                 CloseWindow(gWind);
  73.                 DisposHandle(h);
  74.                 { Remove it from the tool list. }
  75.                 j := 1;
  76.                 for i := 1 to Globals^^.allToolsSize do
  77.                     if Globals^^.allTools[i].tHand <> Globals^^.termHand then
  78.                         begin
  79.                             Globals^^.allTools[j] := Globals^^.allTools[i];
  80.                             j := j+1;
  81.                         end;
  82.                 Globals^^.allToolsSize := Globals^^.allToolsSize-1;
  83.                 SetHandleSize(Handle(Globals),sizeof(OurGlobalType)-sizeof(ToolArray)+
  84.                                             Globals^^.allToolsSize*sizeof(OneToolType));
  85.                 Globals^^.termHand := nil;
  86.             end;
  87.  
  88.         { Find the proc ID. }
  89.         if ParmPresent(1) then GetStrParm(1,toolName)
  90.         else toolName := 'TTY Tool';
  91.         { Look up the requested proc ID. }
  92.         procID := TMGetProcID(toolName);
  93.         if procID = -1 then
  94.             begin
  95.                 { If it wasn't there, then just get the first terminal tool that is there. }
  96.                 if CRMGetIndToolName(ClassTM,1,toolName) <> noErr then procID := -1
  97.                 else procID := TMGetProcID(toolName);
  98.                 { If there are no terminal tools, fail. }
  99.                 if procID = -1 then Fail('Cannot find a default tool');
  100.             end;
  101.  
  102.         { Allocate a window for use by the tool (even though we'll never make it visible). }
  103.         r.top := 0; r.left := 0; r.bottom := 100; r.right := 100;
  104.         { Allocate storage for the WindowRecord. }
  105.         h := NewHandle(sizeof(WindowRecord));
  106.         if h = nil then Fail('Cannot allocate memory');
  107.         MoveHHi(h);
  108.         HLock(h);
  109.         gWind := NewWindow(StripAddress(h^),r,'x',false,noGrowDocProc,nil,false,0);
  110.         if gWind = nil then Fail('Could not create buffer');
  111.         { Create the new connection handle. }
  112.         tHand := TMNew(r,r,tmInvisible+tmNoMenus,procID,gWind,nil,nil,nil,nil,nil,0,ord4(h));
  113.         Globals^^.termHand := tHand;
  114.         if Globals^^.termHand = nil then
  115.             begin
  116.                 CloseWindow(gWind);
  117.                 DisposHandle(h);
  118.                 Fail('Could not create new terminal record');
  119.             end;
  120.         { Save it. }
  121.         Globals^^.allToolsSize := Globals^^.allToolsSize+1;
  122.         { And add it to the outstanding tool list. }
  123.         SetHandleSize(Handle(Globals),sizeof(OurGlobalType)-sizeof(ToolArray)+
  124.                                     Globals^^.allToolsSize*sizeof(OneToolType));
  125.         Globals^^.allTools[Globals^^.allToolsSize].tType := terminalTool;
  126.         Globals^^.allTools[Globals^^.allToolsSize].tHand := Globals^^.termHand;
  127.     end;
  128.  
  129. end.
  130.